package com.fourthGroup.config;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class SentinelConfig {

    @PostConstruct
    public void initFlowRules() {
        List<FlowRule> rules = new ArrayList<>();
        
        // 设置抢购优惠券接口的流量控制规则
        FlowRule rule = new FlowRule();
        rule.setResource("grabCoupon");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS); // 基于QPS限流
        rule.setCount(10); // 每秒允许10个请求
        rules.add(rule);
        
        // 加载规则
        FlowRuleManager.loadRules(rules);
    }
} 